Skip to content

Replace examples with those from the second edition of the AMUSE book#1227

Open
rieder wants to merge 8 commits intoamusecode:mainfrom
rieder:examples_book_v2
Open

Replace examples with those from the second edition of the AMUSE book#1227
rieder wants to merge 8 commits intoamusecode:mainfrom
rieder:examples_book_v2

Conversation

@rieder
Copy link
Member

@rieder rieder commented Mar 10, 2026

Existing examples are outdated, in some cases not working and often duplicated by others.
This replaces all examples from the "textbook", "syllabus" and "simple" directories with the complete set from the textbook (second edition).

@rieder
Copy link
Member Author

rieder commented Mar 10, 2026

To be done:

  • Some tests may use some of the old examples, so these need to be updated
  • Add installable package for the examples (they should be installable as "amuse-examples" via pip, at the least - living in the amuse.examples namespace, as this is how they are used in the book).
  • a file "AMUSE_updates.pdf" needs to be added, as a file containing any errata for the book.
  • missing / later conceived examples may be added later

@rieder
Copy link
Member Author

rieder commented Mar 10, 2026

If we do want to keep (some of) the old examples around, they should be in some directory that makes it clear that these are archived, and not updated. If they are still useful, they should be moved to the new structure.

@LourensVeen
Copy link
Member

I don't see how the tests would depend on the examples, but I'll go and do a local build and run them all to check.

I think pip packages, the PDF, and more examples should be separate Issues, so if it doesn't break the tests then I'll merge this, even if it isn't perfect I doubt it'll make things worse.

Copy link
Member

@LourensVeen LourensVeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit to move the examples to src/amuse_examples and add a forward in src/amuse/examples, and make the whole thing an installable package like the others.

Looking at it again, I think it's probably better to put the actual examples into a subdirectory src/amuse_examples/amuse_examples, so that there's nothing else in the directory. Then we can symlink that to /examples, and if anyone plays with it on the command line then they're less likely to mess up the package definitions.

I've run the tests and this didn't break anything, but then I ran the examples themselves and found many problems, which I've added comments for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crashes with

  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/2017arXiv170307029H_Fig3.py", line 48, in <module>
    from distinct_colours import get_distinct
ModuleNotFoundError: No module named 'distinct_colours'



def anim_gravity(filename="sun_venus_earth.amuse", **kwargs):
particles = read_set_from_file(filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crashes here because the file is not found:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from amuse.examples import run ; run('anim_gravity')
                                     ~~~^^^^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/__init__.py", line 55, in run
    example.main()
    ~~~~~~~~~~~~^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/anim_gravity.py", line 71, in main
    anim_gravity(**kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/anim_gravity.py", line 45, in anim_gravity
    particles = read_set_from_file(filename)
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/io/base.py", line 94, in read_set_from_file
    raise IoException("Error: file '{0}' does not exist.".format(filename))
amuse.io.base.IoException: IO exception: Error: file 'sun_venus_earth.amuse' does not exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think this is actually produced by one of the other examples, so you need to run them in the right order, which likely isn't alphabetical.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails to run using amuse.examples.run:

  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/__init__.py", line 55, in run
    example.main()
    ^^^^^^^^^^^^
AttributeError: module 'amuse_examples.cooling_class' has no attribute 'main'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails to run using amuse.examples.run:

  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/__init__.py", line 55, in run
    example.main()
    ^^^^^^^^^^^^
AttributeError: module 'amuse_examples.gravity_hydro' has no attribute 'main'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails to run using amuse.examples.run:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from amuse.examples import run ; run('gravity_stellar_minimal')
                                     ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/__init__.py", line 55, in run
    example.main()
    ~~~~~~~~~~~~^^
TypeError: main() missing 1 required positional argument: 'args'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails to run using amuse.examples.run because it has no main() function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't run using amuse.examples.run:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from amuse.examples import run ; run('supernova_grid')
                                     ~~~^^^^^^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/__init__.py", line 55, in run
    example.main()
    ~~~~~~~~~~~~^^
TypeError: main() missing 7 required positional arguments: 'stellar_mass', 'stellar_radius', 'core_mass', 'core_radius', 't_end', 'dt', and 'resolution'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails because prepare_figure is missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crashes because we need to explicitly append to a file now:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from amuse.examples import run ; run("three_body_bridge_order4m4")
                                     ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/__init__.py", line 55, in run
    example.main()
    ~~~~~~~~~~~~^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/three_body_bridge_order4m4.py", line 76, in main
    write_set_to_file(ss.savepoint(time), filename, 'hdf5', version='2')
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/io/base.py", line 75, in write_set_to_file
    processor.store()
    ~~~~~~~~~~~~~~~^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/io/store.py", line 99, in store
    processor = store_v2.StoreHDF(
        self.filename,
    ...<4 lines>...
        compression_opts=self.compression_opts
    )
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/io/store_v2.py", line 738, in __init__
    raise FileExistsError("Opening file for write with overwrite_file is False but file {0} exists".format(filename))
FileExistsError: Opening file for write with overwrite_file is False but file SunAndEarthAndMoon_TBB.h5 exists

@@ -0,0 +1,55 @@
from amuse.units import units
from amuse.community.seba import Seba
from seculartriple_tps import Seculartriple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seculartriple_tps is missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Open PRs

Development

Successfully merging this pull request may close these issues.

2 participants